var pageSize=10;
$(function () {
    queryItemList();

    $('#keyWord').on('keypress', function (event) {
        if(event.keyCode == "13"){
            $("#page-list").empty();
            queryItemList();
        }
    });


});


layer_index = layer.load(1, {
    shade: [0.1, '#fff'] //0.1透明度的白色背景
});


/*$.get(
    DocConfig.server+"/api/item/queryList&keyWord="+$("#keyWord").val(),
    {},
    function(data){
        if (data.error_code == 0) {
           // renderItemList(data);
        } else {
            $.alert(lang["save_fail"]);
        }
    },
    "json"

);*/

function queryItemList() {
    var params={
        keyWord: $("#keyWord").val(),
        pageSize:pageSize
    }
    var page=$("#page-list .layui-laypage-curr").text();
    if(page !=''){
        params.pageNo=page;
    }else{
        params.pageNo=1;
    }

    $.ajax({
        url: DocConfig.server + "/api/item/queryList",
        data: params,
        type: 'GET',
        async: true,
        dataType: 'json',
        success: function (result) {
            renderItemList(result);
            renderPage(result);
        }
    })


   $.ajax({
      url: DocConfig.server + "/api/search/search",
      data: params,
      type: 'GET',
      async: true,
      dataType: 'json',
      success: function (result) {
          console.log(result)
          //renderItemList(result);
          //renderPage(result);
      }
  })
}
function getMonth(date){
    var month = "";
    month = date.getMonth() + 1; //getMonth()得到的月份是0-11
    if(month<10){
        month = "0" + month;
    }
    return month;
}
//返回01-30的日期
function getDay(date){
    var day = "";
    day = date.getDate();
    if(day<10){
        day = "0" + day;
    }
    return day;
}
function renderItemList(result) {
    var html = '';
    var json = result.data;
    for (var i = 0; i < json.length; i++) {
        var date=new Date();
        date.setTime(json[i].last_update_time*1000);
        var last_update_time = date.getFullYear()+"-"+getMonth(date)+"-"+getDay(date);//yyyy-MM-dd格式日期
        html += '<li class="fly-list-li">';
        html += '<h2 class="fly-tip">';
        html += '<a target="_blank" href="?s=home/item/show&item_id='+json[i]['item_id']+'">' + json[i]['item_name'] + '</a>';
        html += '</h2>';
        html += '<span class="last-update-time">'+last_update_time;
        html += '</span>';
       /* html += '<p>';
        html += '<span><a href="/member/home/2">' + json[i]['username'] + '</a></span>';
        html += '<span>' + json[i]['addtime'] + '</span>';
        html += '<span>框架教程</span>';
        html += '<span class="fly-list-hint">';
        html += '<i class="iconfont" title="回答"></i> 6';
        html += '<i class="iconfont" title="人气"></i> 2465';
        html += '</span>';
        html += '</p>';*/
        html += '</li> ';
    }
    ;
    $("#item-main-list").html(html);
    //$("#add-item").show();
    layer.closeAll();
    bind_events();

}

function renderPage(result) {
    layui.use('laypage', function(){
        var laypage = layui.laypage;
        //执行一个laypage实例
        laypage.render({
            elem: 'page-list', //注意，这里的 test1 是 ID，不用加 # 号
            count: result.count, //数据总数，从服务端得到
            limit:pageSize,
            curr:result.curr,
            limits:[10, 20, 30, 40, 50],
            jump: function(obj, first){
                //首次不执行
                if(!first){
                    queryItemList();
                }
            }
        });
    });
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function bind_events() {

    //当鼠标放在项目上时将浮现设置和置顶图标
    $(".item-thumbnail").mouseover(function () {
        $(this).find(".item-setting").show();
        $(this).find(".item-top").show();
        $(this).find(".item-down").show();
    });

    //当鼠标离开项目上时将隐藏设置和置顶图标
    $(".item-thumbnail").mouseout(function () {
        $(this).find(".item-setting").hide();
        $(this).find(".item-top").hide();
        $(this).find(".item-down").hide();
    });

    //点击项目设置图标时
    $(".item-setting").click(function () {
        var url = $(this).data("src");
        window.location.href = url;
        return false;
    });

    //点击项目置顶图标时
    $(".item-top").click(function () {

        var action = $(this).data("action");
        var item_id = $(this).data("item_id");
        $.post(
            DocConfig.server + "/api/item/top",
            {"action": action, "item_id": item_id},
            function (data) {
                window.location.reload();
            },
            "json"
        );
        return false;
    });

    //点击取消置顶图标时
    $(".item-down").click(function () {

        var action = 'cancel';
        var item_id = $(this).data("item_id");
        $.post(
            DocConfig.server + "/api/item/top",
            {"action": action, "item_id": item_id},
            function (data) {
                window.location.reload();
            },
            "json"
        );
        return false;
    });

}

/* 轮播 */
var mySwiper = new Swiper ('.swiper-container', {
    pagination: '.pagination',
    paginationClickable :true,
    autoplay : 10000,
    speed:1,

    //autoplayDisableOnInteraction : false,

    onInit: function(swiper){ //Swiper2.x的初始化是onFirstInit
        swiperAnimateCache(swiper); //隐藏动画元素
        swiperAnimate(swiper); //初始化完成开始动画
    },
    onSlideChangeEnd: function(swiper){
        swiperAnimate(swiper); //每个slide切换结束时也运行当前slide动画
    }
})

$('.arrow-left').on('click', function(e){
    e.preventDefault()
    mySwiper.swipePrev()
})
$('.arrow-right').on('click', function(e){
    e.preventDefault()
    mySwiper.swipeNext()
})